//
//  MVTableViewCell.m
//  LuckyRadio
//
//  Created by Mr、J on 15/11/17.
//  Copyright © 2015年 chenjun. All rights reserved.
//

#import "MVTableViewCell.h"
#import "CustomerNetworking.h"
#import "MVModel.h"
#import <UIImageView+WebCache.h>
#import "NewMusicCollectionViewCell.h"
#import "VideoViewController.h"
#import "MVCollectionViewCell.h"
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width

#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

#define WIDTH_PRO(X) (X)*(SCREEN_WIDTH/375)

#define HEIGHT_PRO(Y) (Y)*(SCREEN_HEIGHT/667)

@interface MVTableViewCell ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property (nonatomic, retain) UICollectionView *collectionView;

@property (nonatomic, retain) UICollectionViewFlowLayout *collectionViewFlowLayout;


@end

@implementation MVTableViewCell
- (void)dealloc
{
    [_moreButton release];
    [_titleLabel release];
    [_songsPageModel release];
    [_songsArray release];
    [_collectionView release];
    [_collectionViewFlowLayout release];
    [super dealloc];
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
         self.songsArray = [NSMutableArray array];
        
        [self loadData];
        
        [self createSubViews];
    }
    return self;
}

-(void)loadData
{
   
    [CustomerNetworking customerWithURL:@"http://api.dongting.com/frontpage/frontpage?location=0&version=1447252806&app=ttpod&v=v8.1.1.2015110616&user_id=0&mid=iPhone5S&f=f234&s=s310&imsi=&hid=&splus=8.4.1&active=1&net=2&openudid=e97c1f17f916efa173065a10152c7506e65b60f7&idfa=5FBD2BF5-1484-4C9B-A138-4D9CE22EADB5&utdid=VkKezJUW5SwDAGga0yzrZnyO&alf=700606&bundle_id=com.ttpod.music&latitude=38.89679373280231&longtitude=121.5286730881665" andParamer:nil andBlock:^(id response) {
        
        NSArray *listArray = [NSArray arrayWithArray: [[[response objectForKey:@"data"] objectAtIndex:8] objectForKey:@"data"]];
        for (NSDictionary *dic in listArray) {
            MVModel *mvModel = [MVModel modelWithDictionary:dic];
            [self.songsArray addObject:mvModel];
        }
        
        [self.collectionView reloadData];
    }];
}

-(void)createSubViews
{
    self.collectionViewFlowLayout = [[UICollectionViewFlowLayout alloc] init];
    self.collectionViewFlowLayout.itemSize = CGSizeMake((SCREEN_WIDTH - WIDTH_PRO(40)) / 3, (SCREEN_WIDTH - WIDTH_PRO(40)) / 3 + HEIGHT_PRO(50));
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(WIDTH_PRO(5), HEIGHT_PRO(30), SCREEN_WIDTH - WIDTH_PRO(5), (SCREEN_WIDTH - WIDTH_PRO(40)) / 3 + HEIGHT_PRO(70)) collectionViewLayout:_collectionViewFlowLayout];
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.scrollEnabled = NO;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.collectionView];
    [_collectionViewFlowLayout release];
    [_collectionView release];
    
    [self.collectionView registerClass:[MVCollectionViewCell class] forCellWithReuseIdentifier:@"cellIdentify"];
    
    
    self.titleLabel = [[UILabel alloc] init];
    [self.contentView addSubview:self.titleLabel];
    [_titleLabel release];
    
    self.moreButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.moreButton addTarget:self action:@selector(moreMV:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.moreButton];
    
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    self.titleLabel.frame = CGRectMake(WIDTH_PRO(10), HEIGHT_PRO(10), SCREEN_WIDTH, HEIGHT_PRO(20));
    
    self.moreButton.frame = CGRectMake(SCREEN_WIDTH - WIDTH_PRO(60), HEIGHT_PRO(10), WIDTH_PRO(50), HEIGHT_PRO(20));
    
}
-(void)setSongsPageModel:(SongsPageModel *)songsPageModel
{
    if (_songsPageModel != songsPageModel) {
        [_songsPageModel release];
        _songsPageModel = [songsPageModel retain];
    }
    
    self.titleLabel.text = songsPageModel.name;
    [self.moreButton setTitle:@"更多 >" forState:UIControlStateNormal];
    self.moreButton.titleLabel.font = [UIFont systemFontOfSize:13];
    [self.moreButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
}

+(MVTableViewCell *)mvMusicTableViewCellWith:(UITableView *)tableView
{
    static NSString *cellIdentify = @"newMusic";
    MVTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];
    if (!cell) {
        cell = [[MVTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentify];
    }
    return cell;
    
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    NSLog(@"%ld", _songsArray.count);
    return _songsArray.count;
}

-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 10;
}

-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 10;
}


-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(10, 5, 5, 10);
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MVModel *mvmodel = _songsArray[indexPath.item];
    MVCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentify" forIndexPath:indexPath];
    cell.mvModel = mvmodel;
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    MVModel *mvmodel = _songsArray[indexPath.item];
    [self.delegate pushVideoID:[mvmodel.action objectForKey:@"value"]];
}

- (void)moreMV:(UIButton *)btn
{
    [self.delegate push];
}
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
